* This dofile merges electoral results to the manifesto dataset.

clear

******* 1962 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

// Merge with candidate ID's linked across election years
rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==1962 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

// Manual modifications pre-merge
foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

tempfile tomerge
save `tomerge', replace

// First round
insheet using "$raw_data/manifestos/key_1962.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1962) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1962: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

// Second round
insheet using "$raw_data/manifestos/key_1962.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1962) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1962: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party
tostring id_doc, replace
replace id_doc="id0"+id_doc if length(id_doc)==7
replace id_doc="id"+id_doc if length(id_doc)==8

keep id_doc id_district year dep circ tour cand prenom nom id_1962 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_1962", replace

******* 1967 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==1967 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

tempfile tomerge
save `tomerge', replace

insheet using "$raw_data/manifestos/key_1967.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1967) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1967: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

insheet using "$raw_data/manifestos/key_1967.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1967) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1967: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party
tostring id_doc, replace
replace id_doc="8003010." if id_doc=="8003010" //specific modification to fix an issue with candidate number

replace id_doc="id0"+id_doc if length(id_doc)==7
replace id_doc="id"+id_doc if length(id_doc)==8

keep id_doc id_district year dep circ tour cand prenom nom id_1967 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_1967", replace

******* 1968 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==1968 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

tempfile tomerge
save `tomerge', replace

insheet using "$raw_data/manifestos/key_1968.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1968) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1968: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

insheet using "$raw_data/manifestos/key_1968.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1968) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1968: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party
tostring id_doc, replace
replace id_doc="id0"+id_doc if length(id_doc)==7
replace id_doc="id"+id_doc if length(id_doc)==8

keep id_doc id_district year dep circ tour cand prenom nom id_1968 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_1968", replace

******* 1973 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==1973 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

tempfile tomerge
save `tomerge', replace

insheet using "$raw_data/manifestos/key_1973.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1973) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1973: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

insheet using "$raw_data/manifestos/key_1973.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1973) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1973: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party
tostring id_doc, replace
replace id_doc="id0"+id_doc if length(id_doc)==7
replace id_doc="id"+id_doc if length(id_doc)==8

keep id_doc id_district year dep circ tour cand prenom nom id_1973 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_1973", replace

******* 1978 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==1978 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

tostring dep, replace
replace dep="0"+dep if length(dep)==1
replace dep="2A" if dep=="201"
replace dep="2B" if dep=="202"

foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

tempfile tomerge
save `tomerge', replace

insheet using "$raw_data/manifestos/key_1978.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1978) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1978: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

insheet using "$raw_data/manifestos/key_1978.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1978) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1978: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party
tostring id_doc, replace
replace id_doc="id0"+id_doc if length(id_doc)==7
replace id_doc="id"+id_doc if length(id_doc)==8

keep id_doc id_district year dep circ tour cand prenom nom id_1978 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_1978", replace

******* 1981 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==1981 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

tostring dep, replace
replace dep="0"+dep if length(dep)==1
replace dep="2A" if dep=="201"
replace dep="2B" if dep=="202"

foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

tempfile tomerge
save `tomerge', replace

insheet using "$raw_data/manifestos/key_1981.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1981) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1981: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

insheet using "$raw_data/manifestos/key_1981.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1981) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1981: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party
tostring id_doc, replace
replace id_doc="id0"+id_doc if length(id_doc)==7
replace id_doc="id"+id_doc if length(id_doc)==8

keep id_doc id_district year dep circ tour cand prenom nom id_1981 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_1981", replace

******* 1988 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==1988 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

tostring dep, replace
replace dep="0"+dep if length(dep)==1
replace dep="2A" if dep=="201"
replace dep="2B" if dep=="202"

foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

tempfile tomerge
save `tomerge', replace

insheet using "$raw_data/manifestos/key_1988.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1988) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1988: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

insheet using "$raw_data/manifestos/key_1988.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1988) required(dep circ) gen(match)

so dep circ nom prenom
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1988: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party
tostring id_doc, replace
replace id_doc="id0"+id_doc if length(id_doc)==7
replace id_doc="id"+id_doc if length(id_doc)==8

keep id_doc id_district year dep circ tour cand prenom nom id_1988 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_1988", replace

******* 1993 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==1993 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

tostring dep, replace
replace dep="0"+dep if length(dep)==1
replace dep="2A" if dep=="201"
replace dep="2B" if dep=="202"

foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

replace nom="DELIPKOWSKI" if nom=="LIPKOWSKIDE"
replace nom="DEPERETTI" if nom=="PERETTIDE"
replace nom="DEROCCASERRA" if nom=="ROCCASERRADE"
replace nom="DEBOISGROLLIER" if nom=="BOISGROLLIERDE"
replace nom="DELAVEDAN" if nom=="LAVEDANDE"
replace nom="DUBUISSONDECOURSON" if nom=="BUISSONDUCOURSONDE"
replace nom="DESARS" if nom=="SARSDE"
replace nom="BURGUNDIER" if nom=="BURGUNDER"
replace nom="DEFRANCIOSI" if nom=="FRANCIOSIDE"
replace nom="DEKREGEL" if nom=="KEGELDE"
replace nom="DENICOLAY" if nom=="NICOLAYDE"
replace nom="DANDRE" if nom=="ANDRED"
replace nom="DEPROYART" if nom=="PROYARTDE"
replace nom="MARTINEAUMARTINAUD" if nom=="MARTINAUD"
replace nom="POUILLAUDEAU" if nom=="POUILLAUDE"
replace nom="PONCENAZABAL" if nom=="PONCE"
replace nom="TRENTATOSSI" if nom=="TRENTAROSSI"
replace nom="COUDER" if nom=="COUDERT"
replace nom="BAYET" if nom=="BRAYET"
replace nom="MESJAK" if nom=="MEJSAK"
replace nom="BENHAMIAS" if nom=="BENNAHMIAS"

tempfile tomerge
save `tomerge', replace

insheet using "$raw_data/manifestos/key_1993.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

replace nom="POMARES" if nom=="SUBLET" & prenom=="VINCENT"

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1993) required(dep circ) gen(match)

so dep circ nom prenom
li dep circ nom prenom if _merge!=3
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1993: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

insheet using "$raw_data/manifestos/key_1993.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)

replace circ=2 if circ==1 & (dep=="21" | dep=="31") //modification to fix a mistake in district numbering
replace circ=8 if circ==9 & dep=="67" 
replace nom="QUEYRANNE" if nom=="OUEYRANNE"
replace nom="BROISSIADE" if nom=="DEBROISSIA"

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1993) required(dep circ) gen(match)

so dep circ nom prenom
li dep circ nom prenom if _merge!=3
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1993: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party
tostring id_doc, replace
replace id_doc="id0"+id_doc if length(id_doc)==7
replace id_doc="id"+id_doc if length(id_doc)==8

keep id_doc id_district year dep circ tour cand prenom nom id_1993 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_1993", replace

******* 1997 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==1997 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

tostring dep, replace
replace dep="0"+dep if length(dep)==1
replace dep="2A" if dep=="201"
replace dep="2B" if dep=="202"

foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

tempfile tomerge
save `tomerge', replace

insheet using "$raw_data/manifestos/key_1997.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)
replace nom=subinstr(nom, "'", "", .)
replace prenom=subinstr(prenom, "'", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1997) required(dep circ) gen(match)

so dep circ nom prenom
li dep circ nom prenom if _merge!=3
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1997: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

insheet using "$raw_data/manifestos/key_1997.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace prenom=subinstr(prenom, " ", "", .)
replace nom=subinstr(nom, "'", "", .)
replace prenom=subinstr(prenom, "'", "", .)

reclink dep circ nom prenom using `tomerge', idu(id_unique_cand) idm(id_1997) required(dep circ) gen(match)

so dep circ nom prenom
li dep circ nom prenom if _merge!=3
assert _merge==3

li dep circ nom Unom prenom Uprenom if match>.99 & match<1
li dep circ nom Unom prenom Uprenom if match>.98 & match<.99
li dep circ nom Unom prenom Uprenom if match>.95 & match<.98
li dep circ nom Unom prenom Uprenom if match>.9 & match<.95
li dep circ nom Unom prenom Uprenom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_1997: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party

keep id_doc id_district year dep circ tour cand prenom nom id_1997 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_1997", replace

******* 2017 ***************
use "$intermediate/initial_dataset_cand_lvl", clear

rename first_name_cand _first_name_cand
rename last_name_cand _last_name_cand

merge 1:1 id_unique_cand using "$raw_data/elections/dataset_cand_id_across_elections"
assert _merge==3
drop _merge

keep if year==2017 & election_type==1

rename first_name_cand prenom
rename last_name_cand nom
rename departement_code dep
rename constituency_code circ

tostring dep, replace
replace dep="0"+dep if length(dep)==1
replace dep="2A" if dep=="201"
replace dep="2B" if dep=="202"

foreach x in " " "'" "-"{
replace nom=subinstr(nom, "`x'", "", .)
replace prenom=subinstr(prenom, "`x'", "", .)
}

byso dep circ nom: gen temp=_N
ta temp
li if temp>1

gen doublon=1 if nom=="SALMON" & prenom=="EMERIC" & dep=="35" & circ==1
replace doublon=2 if nom=="SALMON" & prenom=="PHILIPPE" & dep=="35" & circ==1

replace doublon=1 if nom=="THOMAS" & prenom=="FREDERIC" & dep=="54" & circ==6
replace doublon=2 if nom=="THOMAS" & prenom=="RACHEL" & dep=="54" & circ==6

replace doublon=1 if nom=="DUMONT" & prenom=="JEROME" & dep=="55" & circ==2
replace doublon=2 if nom=="DUMONT" & prenom=="JEANLOUIS" & dep=="55" & circ==2

replace doublon=1 if nom=="LAURENT" & prenom=="CHRISTINE" & dep=="59" & circ==21
replace doublon=2 if nom=="LAURENT" & prenom=="JEANLUCFRANCOIS" & dep=="59" & circ==21

replace doublon=1 if nom=="LETARD" & prenom=="LINE" & dep=="973" & circ==1
replace doublon=2 if nom=="LETARD" & prenom=="SYLVIOLEON" & dep=="973" & circ==1

replace doublon=1 if nom=="HOARAU" & prenom=="CLEMENDEAU" & dep=="974" & circ==3
replace doublon=2 if nom=="HOARAU" & prenom=="JACQUET" & dep=="974" & circ==3

replace doublon=1 if nom=="VALLON" & prenom=="JEANPAUL" & dep=="07" & circ==2
replace doublon=1 if nom=="GOASGUEN" & prenom=="CLAUDE" & dep=="75" & circ==14
replace doublon=1 if nom=="MARTIN" & prenom=="DANIEL" & dep=="77" & circ==9
replace doublon=1 if nom=="RIMANE" & prenom=="JULIANA" & dep=="973" & circ==2

drop temp

tempfile tomerge
save `tomerge', replace

insheet using "$raw_data/manifestos/key_2017.csv", clear

local size=_N

keep if tour==1

replace nom=subinstr(nom, " ", "", .)
replace nom=subinstr(nom, "_", "", .)

byso dep circ tour nom: gen temp=_N
ta temp
li if temp>1

gen doublon=1 if nom=="SALMON" & temp==2 & cand==16
replace doublon=2 if nom=="SALMON" & temp==2 & cand==17

replace doublon=1 if nom=="THOMAS" & temp==2 & cand==13
replace doublon=2 if nom=="THOMAS" & temp==2 & cand==7

replace doublon=1 if nom=="DUMONT" & temp==2 & cand==3
replace doublon=2 if nom=="DUMONT" & temp==2 & cand==8

replace doublon=1 if nom=="LAURENT" & temp==2 & cand==12
replace doublon=2 if nom=="LAURENT" & temp==2 & cand==8

replace doublon=1 if nom=="LETARD" & temp==2 & cand==7
replace doublon=2 if nom=="LETARD" & temp==2 & cand==8

replace doublon=1 if nom=="HOARAU" & temp==2 & cand==7
replace doublon=2 if nom=="HOARAU" & temp==2 & cand==9

replace doublon=1 if nom=="VALLON" & dep=="07" & circ==2
replace doublon=1 if nom=="GOASGUEN" & dep=="75" & circ==14
replace doublon=1 if nom=="MARTIN" & dep=="77" & circ==9
replace doublon=1 if nom=="RIMANE" & dep=="973" & circ==2

drop temp

reclink dep circ nom doublon using `tomerge', idu(id_unique_cand) idm(id_2017) required(dep circ) gen(match)

so dep circ nom
li dep circ nom if _merge!=3
assert _merge==3

li dep circ nom Unom if match>.99 & match<1
li dep circ nom Unom if match>.98 & match<.99
li dep circ nom Unom if match>.95 & match<.98
li dep circ nom Unom if match>.9 & match<.95
li dep circ nom Unom if match<.9

replace match=. if dep=="75" & circ==14 & (Unom=="LOUY" | Unom=="LAFONT")

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_2017: gen temp=_N
ta temp
assert temp==1
drop temp

tempfile toappend
save `toappend', replace

insheet using "$raw_data/manifestos/key_2017.csv", clear

keep if tour==2

replace nom=subinstr(nom, " ", "", .)
replace nom=subinstr(nom, "_", "", .)

gen doublon=2 if nom=="HOARAU" & dep=="974" & circ==3
replace doublon=1 if nom=="GOASGUEN" & dep=="75" & circ==14

byso dep circ nom: gen temp=_N
ta temp
li if temp>1
drop temp

reclink dep circ nom doublon using `tomerge', idu(id_unique_cand) idm(id_2017) required(dep circ) gen(match)

so dep circ nom
li dep circ nom if _merge!=3
assert _merge==3

li dep circ nom Unom if match>.99 & match<1
li dep circ nom Unom if match>.98 & match<.99
li dep circ nom Unom if match>.95 & match<.98
li dep circ nom Unom if match>.9 & match<.95
li dep circ nom Unom if match<.9

keep if match!=.

byso id_unique_cand: gen temp=_N
ta temp
assert temp==1
drop temp
byso id_2017: gen temp=_N
ta temp
assert temp==1
drop temp

append using `toappend'
assert _N==`size'

rename political_orientation_cand orientation
rename party_name party
tostring id_doc, replace
replace id_doc="id0"+id_doc if length(id_doc)==7
replace id_doc="id"+id_doc if length(id_doc)>=8

keep id_doc id_district year dep circ tour cand prenom nom id_2017 id_unique_cand* orientation party coalition1 coalition2

save "$intermediate/key_augmented_2017", replace

